﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clandestine.Entities
{
    public class Chest : Entity
    {
        private AnimatedSprite sprite = new AnimatedSprite();

        public override Sprite Sprite
        {
            get { return (Sprite)sprite; }
        }

        public override bool CanUse
        {
            get
            {
                return true;
            }
        }

        public Direction direction = Direction.Down;
        public Direction Direction
        {
            get { return direction; }
            set
            {
                direction = value;
                setAnimation();
            }
        }

        private string item = string.Empty;
        public string Item
        {
            get { return item; }
            set { item = value; }
        }

        private bool opened = false;
        public bool Opened 
        {
            get { return opened; }
            set 
            { 
                opened = value;
                setAnimation();
            }
        }

        public Chest()
        {
            sprite.AddAnimations("chest.png", "sprite_metadata/chest.xml");
            setAnimation();

            this.UseRadius = 0;
            this.CollisionOffsetTop = 7;
            this.CollisionOffsetBottom = 7;
        }

        private void setAnimation()
        {
            // Make sure we're using the correct animation
            switch (Direction)
            {
                case Direction.Up:
                    sprite.SetAnimation("up");
                    break;
                case Direction.Down:
                    sprite.SetAnimation("down");
                    break;
                case Direction.Right:
                    sprite.SetAnimation("right");
                    break;
                case Direction.Left:
                    sprite.SetAnimation("left");
                    break;
            }

            // Opened or closed?
            sprite.CurrentFrame = (Opened) ? 3 : 0;
        }

        public override void OnUse(int playerXPos, int playerYPos)
        {
            if (!opened)
            {
                // Graphically
                opened = true;
                sprite.CurrentAnimation.Loop = false;
                sprite.Playing = true;

                string message = "There's nothing in here.";

                // Logic -- i.e. the item.
                if (item != "")
                {
                    Clandestine.Items.Item i = Items.ItemDatabase.CreateItem(item);
                    WorldMap.Player.Inventory.AddItem(i);
                    message = String.Format("You have found {0} {1}!", i.IndefiniteArticle, i.Name);
                }

                // Message
                MessageWindow mw = new MessageWindow(message);
                mw.ShowModally();
            }
        }

        public override void Dispose()
        {
            sprite.Dispose();
        }
    }
}
